package com.cloud.aiservice.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.cloud.aiservice.config.DynamicDataSourceHolder;
@Component
@Order(value = -100)
@Aspect
public class DataSourceAspect {
	protected Logger log = LoggerFactory.getLogger(DataSourceAspect.class);
	//配置数据源
	/**
	 * 切入写数据库注解
	 * 	 */
    @Pointcut(
    		" @annotation(org.apache.ibatis.annotations.Insert)|| @annotation(org.apache.ibatis.annotations.Update) ")
    private void dbMasterAspect() {
    }
	/**
	 * 切入读数据库注解
	 */
    @Pointcut("@annotation(org.apache.ibatis.annotations.Select)")
    private void dbSlaveAspect() {
    }
    
    @Before("dbMasterAspect()")
    public void db1() {
        log.info("切换到DB_SLAVE 数据源...");
        DynamicDataSourceHolder.setDBType(DynamicDataSourceHolder.DB_SLAVE);
    }
 
    @Before("dbSlaveAspect()")
    public void db2() {
        log.info("切换到DB_MASTER 数据源...");
        DynamicDataSourceHolder.setDBType(DynamicDataSourceHolder.DB_MASTER);
    }
    
}
